﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GameService;

namespace Website
{
    public partial class login : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["guest"] == "1")
            {
                string name = Master.GameManager.Guest_Connection();
                Master.SetUser(name);
                Master.IsGuest = true;
                Response.Redirect("gamelist.aspx");
            }

        }

        protected void Confirm_Click(object sender, EventArgs e)
        {
            bool success = false;

            string username = txtUserName.Text;
            string password = txtPassword.Text;

            AuthenticationResult authentication_Res = Master.GameManager.Authenticate(username, password);
            if (authentication_Res == AuthenticationResult.IncorrectUserName || 
                authentication_Res ==AuthenticationResult.IncorrectPassword)
                lblMessage.Text = "Incorrect username or password";
            else if (authentication_Res == AuthenticationResult.UserAlreadyLoggedIn)
                lblMessage.Text = "User is already logged in";
            else if (authentication_Res == AuthenticationResult.UnknownError)
                lblMessage.Text = "Login failed due to unknown reason";
            else
                success = true;


            if (success)
            {
                Master.SetUser(username);
                Response.Redirect("gamelist.aspx");
            }
            else
            {
                lblMessage.Visible = true;
            }

        }
    }
}
